package com.chuyu.jzwboa;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chuyu.jzwboa.activity.message.MsgSetActivity;
import com.chuyu.jzwboa.base.MVPBaseActivity;
import com.chuyu.jzwboa.fragment.main.MainFragment;
import com.chuyu.jzwboa.fragment.message.MsgFragment;
import com.chuyu.jzwboa.fragment.my.MyFragment;
import com.chuyu.jzwboa.fragment.search.SearchFragment;
import com.chuyu.jzwboa.presenter.main.MainPresenter;
import com.chuyu.jzwboa.utils.popupwindow.PopupWindowUtil;
import com.chuyu.jzwboa.utils.sys.StatusBarUtils;
import com.chuyu.jzwboa.utils.sys.ToastUtils;
import com.chuyu.jzwboa.view.iview.main.IMainView;
import com.chuyu.jzwboa.widget.MainVpagerView;
import com.chuyu.jzwboa.widget.NoScrollViewPager;
import com.facebook.drawee.view.SimpleDraweeView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends MVPBaseActivity<IMainView, MainPresenter>
		implements IMainView {


	@BindView(R.id.titel_left_img)
	SimpleDraweeView titelLeftImg;
	@BindView(R.id.title_text)
	TextView titleText;
	@BindView(R.id.title_right_img)
	SimpleDraweeView titleRightImg;
	@BindView(R.id.titel_rl)
	RelativeLayout titelRl;
	@BindView(R.id.main_viewpager)
	NoScrollViewPager mainViewpager;
	@BindView(R.id.tab_main)
	RadioButton tabMain;
	@BindView(R.id.tab_msg)
	RadioButton tabMsg;
	@BindView(R.id.ll_add)
	LinearLayout llAdd;
	@BindView(R.id.tab_search)
	RadioButton tabSearch;
	@BindView(R.id.tab_me)
	RadioButton tabMe;
	@BindView(R.id.main_tab_bottom)
	RadioGroup mainTabBottom;
	private MainVpagerView mainVpagerView;
	private int radioBtIndex = -1;

	@Override
	public void loadSuccess() {

	}

	@Override
	public void loadFailed() {

	}

	@Override
	public void loadException() {

	}

	@Override
	protected int initContent() {
		return R.layout.activity_main;
	}

	@Override
	protected void initView() {


		MainFragment mainFragment = new MainFragment();
		MsgFragment msgFragment = new MsgFragment();
		MyFragment myFragment = new MyFragment();
		SearchFragment searchFragment = new SearchFragment();
		Fragment[] fragment = {mainFragment, msgFragment, searchFragment, myFragment};

		mainVpagerView = new MainVpagerView(this, tabMain, tabMsg, tabSearch,
				tabMe, mainTabBottom, mainViewpager, fragment, titleText);


	}

	@Override
	protected void initData() {
		/**
		 * 动态设置状态栏高度
		 */
		int statusHeight = StatusBarUtils.getStatusHeight(this);
		titelRl.setPadding(0, statusHeight, 0, 0);
		/**
		 * 初始化第一个选中
		 */
		mainVpagerView.setTabs();
		mainVpagerView.initFragmentViewPager();
		//设置viewpager选中
		mainViewpager.setCurrentItem(1);
		//radiogroup
		mainTabBottom.check(R.id.tab_main);
	}

	@NonNull
	@Override
	public MainPresenter createPresenter() {
		return new MainPresenter();
	}

	/**
	 * 设置main中fragment的选中位置
	 *
	 * @param index
	 */
	public void setFragementIndex(int index) {
		radioBtIndex = index;
		Log.i("test", "调用:" + index);
		switch (index) {
			case 0:
				//首页
				titelLeftImg.setVisibility(View.GONE);
				titleRightImg.setVisibility(View.VISIBLE);
				titleRightImg.setImageResource(R.mipmap.ic_launcher_round);
				break;
			case 1:
				//消息界面
				titelLeftImg.setVisibility(View.GONE);
				titleRightImg.setVisibility(View.VISIBLE);
				titleRightImg.setImageResource(R.mipmap.ic_launcher);
				break;
			case 2:
				titleRightImg.setVisibility(View.GONE);
				titleRightImg.setVisibility(View.GONE);
				break;
			case 3:
				titleRightImg.setVisibility(View.GONE);
				titleRightImg.setVisibility(View.GONE);
				break;
			default:
				break;
		}
	}


	@Override
	public void onBackPressed() {
		if (PopupWindowUtil.getInstance()._isShowing()) {
			PopupWindowUtil.getInstance()._rlClickAction();
		} else {
			super.onBackPressed();
		}
	}

	@Override
	public void onPointerCaptureChanged(boolean hasCapture) {

	}

	@OnClick({R.id.title_right_img, R.id.ll_add})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.ll_add:
				PopupWindowUtil.getInstance()._show(getApplicationContext(), llAdd);
				break;
			case R.id.title_right_img:
				Log.i("test", "点击:" + radioBtIndex);
				switch (radioBtIndex) {
					case 0:
						ToastUtils.show(this, "首页");
						break;
					case 1:
						Intent intent = new Intent(this, MsgSetActivity.class);
						startActivity(intent);
						break;
					default:
						break;
				}
				break;

			default:
				break;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// TODO: add setContentView(...) invocation
		ButterKnife.bind(this);
	}
}
